package com.rankeiot.platform.controller;


import com.rankeiot.core.data.response.Resp;
import com.rankeiot.platform.domain.Detection;
import com.sun.management.OperatingSystemMXBean;


import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.management.MemoryMXBean;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("detector")

public class DetectorController implements  Runnable {
    //@Inject
    //AutoBox auto;
    @Autowired
    private SqlToyLazyDao dao;
   // private FlowHelper flowHelper;
    private OperatingSystemMXBean osmxb;
    MemoryMXBean memoryMXBean;
    private final int SECOND_OF_DAY = 3600 * 24;
    private int period = 10;

//    @Init
//    public void init() {
//        /**/
//        flowHelper = new FlowHelper(FlowType.Second);
//        osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
//        memoryMXBean = ManagementFactory.getMemoryMXBean();
//        SolonApp app = Solon.app();
//        app.filter(this);
//       // Utils.scheduled.scheduleAtFixedRate(this, 0, period, TimeUnit.SECONDS);
//        RunUtil.delayAndRepeat(this,period*1000);
//        /**/
//    }

//    @Get
//    @Mapping("list")
    public Resp<List<Detection>> list() {
//        long t=System.currentTimeMillis()/1000-SECOND_OF_DAY;
//        long ct=auto.count("from Detection");
//        System.out.println(ct);
//        List<Detection> list = auto.select(Detection.class, "from Detection where Time>?",t);
        return Resp.of(new ArrayList<>());
    }

    /**
     * qps过滤器
     *
     * @throws Throwable
     */
//    @Override
//    public void doFilter(Context ctx, FilterChain chain) throws Throwable {
//        long start = System.currentTimeMillis();
//        try {
//            chain.doFilter(ctx);
//            flowHelper.incrSuccess(System.currentTimeMillis() - start);
//        } catch (Throwable e) {
//            flowHelper.incrException();
//            throw e;
//        }
//    }

    private Detection getCurrent() {
        Detection detection = new Detection();

//        Flower flower = flowHelper.getFlow(FlowType.Second);
//        detection.setAvgRt(flower.avgRt());
//        detection.setQps(flower.total());
//
//
//        detection.setCpu(format(osmxb.getProcessCpuLoad() * 100));
//
//        MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
//        detection.setMemory(format(memoryUsage.getUsed() / (1024 * 1024d)));

        return detection;
    }

    private double format(double v){
        BigDecimal bd=new BigDecimal(v);
        return bd.setScale(2, RoundingMode.HALF_UP).doubleValue();
    }
    @Override
    public void run() {
        Date time = new Date();
        long t = time.getTime() / 1000;
//        try (Box box = auto.cube()) {
//            // 如果在0点
//            if (t % SECOND_OF_DAY < period) {
//                BList<Detection> toDelete = box.select(Detection.class, "from Detection where Time<?", t - 3 * SECOND_OF_DAY);
//                for (Detection d : toDelete) {
//                    box.d("Detection", d.getTime()).delete();
//                }
//            }
//            Detection detection = getCurrent();
//            detection.setTime(t);
//            box.d("Detection").insert(detection);
//            box.commit();
//        }
    }
}
